package fanjingzhu.check.dao.impl;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import publics.model.Enterprise;
import publics.model.Proposer;
import fanjingzhu.check.dao.ProposerDao;
@Component("proposerDao")
public class ProposerDaoImpl implements ProposerDao{
	
	HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
    @Resource(name="hibernateTemplate")
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
		System.out.println("hibernateTemplate injected in proposerDaoImpl!");
	}
    public Integer save(Proposer proposer) {
		return (Integer)this.getHibernateTemplate().save(proposer);
	}
    
	public boolean delete(Proposer proposer) {
		try{
		this.getHibernateTemplate().delete(proposer);
		}catch(DataAccessException e){
		    return false;
		}
		return true;
	}

	public boolean deleteById(Integer proposerId) {
		try{
			this.getHibernateTemplate().delete(this.getHibernateTemplate().get(Proposer.class, proposerId));
			}catch(DataAccessException e){
			    return false;
			}
			return true;
	}
	
	public boolean update(Proposer proposer) {
		try{
			this.getHibernateTemplate().update(proposer);
		}catch(DataAccessException e){
			return false;
		}
		return true;
	}
	
	public int findByCount(String sql) {
		return this.getHibernateTemplate().find(sql).size();
	}

	public Proposer findById(Integer proposerId) {
		return this.getHibernateTemplate().get(Proposer.class, proposerId);
	}
	@SuppressWarnings("unchecked")
	public List<Proposer> findByPage(final String hql, final int offset, final int pageSize) {
		List<Proposer> list=this.getHibernateTemplate().executeFind(
				new HibernateCallback(){
					public Object doInHibernate(Session session) throws HibernateException ,SQLException {
					//执行hibernate分页查询
					List<Enterprise> result = session.createQuery(hql)
					.setFirstResult(offset)
					.setMaxResults(pageSize)
					.list();
					return result;
					}
				}	
				);
		return list;
    }
}
